/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.uc.controller;

import com.xy.biz.uc.client.TeamClient;
import com.xy.biz.uc.client.UserClient;
import com.xy.biz.uc.domain.bo.TeamUserRelBO;
import com.xy.biz.uc.domain.bo.TeamBO;
import com.xy.biz.uc.domain.converter.TeamConverter;
import com.xy.biz.uc.domain.converter.UserTeamConverter;
import com.xy.biz.uc.domain.query.TeamQuery;
import com.xy.biz.uc.domain.converter.TeamUserRelConverter;
import com.xy.biz.uc.domain.req.*;
import com.xy.biz.uc.domain.resp.GetTeamResponse;
import com.xy.biz.uc.domain.resp.GetUserTeamResponse;
import com.xy.biz.uc.domain.resp.GetTeamUserResponse;
import com.xy.biz.uc.service.TeamService;
import com.xy.lang.domain.Result;
import com.xy.lang.util.CalendarUtils;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 *
 *
 * @author yzh
 * @date 2021-4-26
 */
@Api(tags = "团队管理")
@Slf4j
@RestController
@RequestMapping("/api/uc/team")
public class TeamController implements TeamClient {

    @Autowired
    private TeamService teamService;

    @Autowired
    private UserClient userClient;

    @ApiOperation("保存团队")
    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save")
    public Result<?> saveTeam(@RequestBody SaveTeamRequest request) {
        return teamService.saveTeam(request);
    }

    @ApiOperation("修改团队")
    @Validatable(groups = {UpdateChecks.class})
    @PostMapping(value = "/update")
    public Result<?> updateTeam(@RequestBody SaveTeamRequest request) {
        return teamService.updateTeam(request);
    }

    @ApiOperation("删除团队")
    @Validatable
    @PostMapping(value = "/delete")
    public Result<?> deleteTeam(@RequestBody GetTeamRequest request) {
        teamService.deleteTeam(request.getTeamId());
        return Result.success();
    }

    @ApiOperation("获取团队")
    @Validatable
    @PostMapping(value = "/get")
    @Override
    public Result<GetTeamResponse> getTeam(@RequestBody GetTeamRequest request) {
        return Result.success(TeamConverter.toResponse(teamService.getTeam(request.getTeamId())));
    }

    @PostMapping(value = "/list_by_ids")
    @Override
    public Result<List<GetTeamResponse>> listTeamById(@RequestBody Collection<Long> collection) {
        return Result.success(TeamConverter.toResponse(teamService.listTeamById(collection)));
    }

//	@Deprecated
//	@PostMapping(value = "/list")
//	@Override
//	public Result<List<GetTeamResponse>> listTeam(@RequestBody ListTeamRequest request) {
//		return Result.success(TeamConverter.toResponse(teamService.listTeam(TeamConverter.toQuery(request))));
//	}
//
//	@Deprecated
//	@PostMapping(value = "/count")
//	@Override
//	public Result<Integer> countTeam(@RequestBody ListTeamRequest request) {
//		return Result.success(teamService.countTeam(TeamConverter.toQuery(request)));
//	}

    @ApiOperation("查询加入的团队")
    @PostMapping(value = "/list_joined")
    @Validatable
    @Override
    public Result<List<GetTeamResponse>> listJoinedTeam(@RequestBody ListTeamRequest request) {
        return Result.success(teamService.listJoinedTeam(request));
    }

    @ApiOperation("添加团队成员")
    @Validatable
    @PostMapping(value = "/invite_join")
    public Result<?> inviteJoin(@RequestBody SaveTeamUserRelRequest request) {
        return teamService.inviteJoin(request);
    }

    @ApiOperation("修改团队用户身份")
    @Validatable
    @PostMapping(value = "/update_team_user_role")
    public Result<?> updateTeamUserRole(@RequestBody UpdateTeamUserRelRequest request) {
        return teamService.updateTeamUserRole(request);
    }

    @ApiOperation("删除团队用户身份")
    @Validatable
    @PostMapping(value = "/deleted_team_user")
    public Result<?> deletedTeamUser(@RequestBody DeleteTeamUserRelRequest request) {
        return teamService.deletedTeamUserRole(request);
    }

    @ApiOperation("获取团队用户列表")
    @Validatable
    @PostMapping(value = "/list_team_user")
    public Result<List<GetTeamUserResponse>> listTeamUser(@RequestBody ListTeamUserRelRequest request) {
        List<TeamUserRelBO> teamUserRelBOS = teamService.listTeamUser(request);
        if (!CollectionUtils.isEmpty(teamUserRelBOS)) {
            List<GetTeamUserResponse> getTeamUserResponseList = TeamUserRelConverter.toResponse(teamUserRelBOS);
            userClient.buildByUser(getTeamUserResponseList,
                    getTeamUserResponseList.stream().map(e -> e.getUserId()).collect(Collectors.toSet()),
                    GetTeamUserResponse::getUserId,
                    (getTeamUserResponse, userDTO) -> getTeamUserResponse.setUserName(userDTO.getUserName()));
            getTeamUserResponseList.stream().forEach(item -> {
                item.setJoinTimeStr(CalendarUtils.getDeadlineString(item.getJoinTime()));
            });
            return Result.success(getTeamUserResponseList);
        }
        return Result.success(Collections.emptyList());
    }

    @ApiOperation("判断当前用户是否是团队管理员")
    @Validatable
    @PostMapping(value = "/is_team_admin")
    public Result<Boolean> isTeamAdmin(@RequestBody GetTeamRequest request) {
        return teamService.isTeamAdmin(request.getTeamId());
    }

    @Override
    @ApiOperation("批量判断当前用户是否是团队管理员")
    @Validatable
    @PostMapping(value = "/is_team_admins")
    public Result<Set<Long>> isTeamAdminS(@RequestBody GetTeamSRequest request) {
        return teamService.isTeamAdminS(request.getTeamIds());
    }

}
